/**
 * Created by sxwu on 14-2-15.
 */


var log = require("../lib/log");
var config = require('../config');
var virapinfo = require('../models/virapinfo');
var crypto = require('crypto');
var RUtil = require('./dataformatUtil');
//var domain = require('domain');
var virwifiinfo = require('../models/virwifiinfo');

/*var d = domain.create();


d.on('error', function(e) {
    console.log("error in domain:", e);
});*/



exports.index = function(request, response){
    request.session.lastpage="setbindap.html";
    if(request.session.admin==undefined)
    {
        response.redirect("login.html");
    }
    else
    {

        var method = request.method || '';
        var data = RUtil.ap_data(request);
        data.front_data.title = '基本配置';
        data.front_data.subtitle='AP设备绑定';
        data.front_data.usernick =request.session.admin.usernick;
        data.front_data.usertype =request.session.admin.usertype;
        data.front_data.status="danger";

        if(method.toUpperCase() === "GET") {
            virapinfo.selectAPWiFi(request.session.admin.userid, function(error,results) {
                if((error)!== null)
                {
                    // 数据库异常
                    data.front_data.errDesp = "查询失败：系统内部错误(错误码："+error.errno+")";
                    data.front_data.status="danger";
                    response.render("setbindap", data);

                }
                else if(results.length <= 0) {
                    // 不存在，直接响应页面
                    data.front_data.errDesp = "您还没有添加任何AP设备，马上开始添加吧";
                    data.front_data.status="success";
                    response.render("setbindap", data);
                } else {
                    //存在数据将数据发到页面
                    data.apinfos=JSON.stringify(results);
                    response.render("setbindap", data);
                }
            });
        }

        else if(method.toUpperCase() === "POST") {
            //post需要区分不同的oper类型

            data.apinfo.userid = request.session.admin.userid;
            if(request.body.oper == "add")
            {
                data.apinfo.userid=request.session.admin.userid;

                data.apinfo.apname=request.body.apname;
                data.apinfo.apmac=request.body.apmac;
                data.apinfo.apsn=request.body.apsn;
                //data.apinfo.apdes=request.body.apdes;
                //data.apinfo.aplocation=request.body.aplocation;

                data.apinfo.groupenable=request.body.groupenable;
                if(data.apinfo.groupenable==1)
                {
                    data.apinfo.devid=request.body.devid;
                    data.apinfo.groupidforssid=request.body.groupidforssid;
                }


                virwifiinfo.select(request.session.admin.userid, function(error,results) {
                    var status="error";
                    if((error)!== null)
                    {
                        // 数据库异常
                        data.front_data.errDesp = "添加失败：系统内部错误(错误码："+error.errno+")";
                        var info = { status:status,info:data.front_data.errDesp };
                        response.write(JSON.stringify(info));
                        response.end();
                    }
                    else if(results.length <= 0) {
                        // 不存在，直接响应页面
                        data.front_data.errDesp = "添加失败：没有任何WiFi信息，请先配置WiFi！";
                        var info = { status:status,info:data.front_data.errDesp };
                        response.write(JSON.stringify(info));
                        response.end();
                    } else {
                        //存在数据将数据发到页面
                        var tmpwifiinfo = results[0];
                        data.apinfo.wifiid = tmpwifiinfo.wifiid;
                        virapinfo.insert(data.apinfo, function(error,results) {
                            if((error)!== null)
                            {
                                // 数据库异常
                                data.front_data.errDesp = "添加失败：系统内部错误(错误码："+error.errno+")";
                            }
                            else if(results.length <= 0) {
                                //没有该虚拟运营商的配置信息，响应空的内容
                                data.front_data.errDesp = "添加失败：数据库操作异常！";
                            } else {
                                data.front_data.errDesp="AP设备插入成功！";
                                status="success";
                            }
                            var info = { status:status,info:data.front_data.errDesp };
                            response.write(JSON.stringify(info));
                            response.end();
                        });

                    }
                });
            }
            else if(request.body.oper == "edit")
            {
                //data.apinfo.userid=request.session.admin.userid;
                data.apinfo.apid=request.body.apid;
                data.apinfo.wifiid=request.body.wifiid;
                data.apinfo.apname=request.body.apname;
                data.apinfo.apmac=request.body.apmac;
                data.apinfo.apsn=request.body.apsn;
                //data.apinfo.apdes=request.body.apdes;
                //data.apinfo.aplocation=request.body.aplocation;
                //data.apinfo.apcustomernum=request.body.apcustomernum;
                //data.apinfo.apflag=request.body.apflag;
                data.apinfo.groupenable=request.body.groupenable;
                if(data.apinfo.groupenable==1)
                {
                    data.apinfo.devid=request.body.devid;
                    data.apinfo.groupidforssid=request.body.groupidforssid;
                }


                virapinfo.update(data.apinfo, function(error,results) {
                    var status="error";
                    if((error)!== null)
                    {
                        // 数据库异常
                        data.front_data.errDesp = "更新失败：系统内部错误(错误码："+error.errno+")";
                    }
                    else if(results.length <= 0) {
                        data.front_data.errDesp = "更新失败：系统内部错误(错误码："+error.errno+")";
                    } else {
                        status="success";
                        data.front_data.errDesp="AP设备更新成功！";
                    }
                    var info = { status:status,info:data.front_data.errDesp };
                    response.write(JSON.stringify(info));
                    response.end();
                });
            }
            else  if(request.body.oper == "del")
            {
                var delid= new Array();
                delid = request.body.id.split(',');
                virapinfo.delete(delid, function(error,results) {
                    var status="error";
                    if((error)!== null)
                    {
                        // 数据库异常
                        data.front_data.errDesp = "删除失败：系统内部错误(错误码："+error.errno+")";
                    }
                    else if(results.length <= 0) {
                        data.front_data.errDesp = "删除失败：系统内部错误(错误码："+error.errno+")";
                    } else {
                        status="success";
                        data.front_data.errDesp="AP设备删除成功！";
                    }
                    var info = { status:status,info:data.front_data.errDesp };
                    response.write(JSON.stringify(info));
                    response.end();
                });
            }





        }
    }
};
